package com;

import java.util.Date;
import java.util.Scanner;

/**
 * 线程同步就是共享资源
 */
public class syncone implements Runnable {
    private int num = 1;
    private int size = 10;





    @Override
    public void run() {

        while (true){
            //同步块
            synchronized (this){
            if(num >= size){
                System.out.println("售卖结束");
                while (Thread.currentThread().isAlive()){
                    System.out.println( Thread.currentThread().isInterrupted());
                    System.out.println( Thread.currentThread().isAlive());
                    System.out.println("回收妹子:"+Thread.currentThread().getName());
                    Thread.currentThread().stop();
                    Thread.currentThread().interrupt();
                    System.out.println( Thread.currentThread().isInterrupted());
                    System.out.println( Thread.currentThread().isAlive());
                }
                break;
            }
            System.out.println(String.format("时间:%s,售票妹子:%s，卖出第:%s 张票，还剩下:%s 张票",new Date(),Thread.currentThread().getName(),this.num,this.size-this.num));
            try {
                Thread.sleep(500);
                num ++ ;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }}
    }

    public static void main(String[] args) {
        syncone three = new syncone();
        while (true){
            System.out.println("输入售卖妹妹");
            String girl  = new Scanner(System.in).next();

            Thread thread = new Thread(three,girl);
            thread.start();

        }
    }
}
